<?php

namespace App\Service;

use App\Models\UnlockRecordModel;
use Carbon\Carbon;

class UnlockService
{
    public function checkUnlock($userId, $businessIds, $businessType)
    {
        $records = UnlockRecordModel::getByBusiness($userId, $businessIds, $businessType);
        $checkRes = [];
        foreach ($businessIds as $businessId) {
            $checkRes[$businessId] = false;
            foreach ($records as $record) {
                if ($businessId == $record['business_id']) {
                    $checkRes[$businessId] = true;
                }
            }
        }
        return $checkRes;
    }

    public function unlockCharacter($userId, $characterId, $unlockType, $unlockValue)
    {
        $params = [
            'user_id' => $userId,
            'business_type' => 1,
            'business_id' => $characterId,
            'unlock_type' => $unlockType,
            'unlock_value' => $unlockValue,
            'unlock_time' => Carbon::now()->toDateTimeString()
        ];
        return UnlockRecordModel::commit($params);
    }

    public function unlockImage($userId, $imageId, $unlockType, $unlockValue)
    {
        $params = [
            'user_id' => $userId,
            'business_type' => 2,
            'business_id' => $imageId,
            'unlock_type' => $unlockType,
            'unlock_value' => $unlockValue,
            'unlock_time' => Carbon::now()->toDateTimeString()
        ];
        return UnlockRecordModel::commit($params);
    }
}
